#!/bin/sh  

# The following line, below is all I need for a quick build/test. I'll clean up this
# file next time I use it.
sudo dpkg -r carblogger_ui; cmake .. && make -j2 package && sudo dpkg  -i carblogger_ui-0.1.1-Linux.deb && sudo /etc/init.d/carblogger_ui.sh start

# All startup functionality was moved to /etc/init.d/carblogger_ui. So exit
exit 0

# Round trip test. Most of this can go in CMakeLists.txt but just doing a 
# quick to test to all the files where I need them (for external css).


# Ensure this script isn't already running. 
# Note, it checks by name so if another script by the same name is running, this will give a false positive.
# TODO replace build.sh with $0
# ps -o pid -C build.sh
# A=`ps -o pid -C "$0" | wc -w` 
# echo $A
# exit 1
# 
# if [ `ps -o pid -C build.sh | wc -l` != "  PID" ]; then
#     echo "build.sh is already running. Exiting."
#     exit 1;
# fi
if [ -f /tmp/gui_build_script.running ]; then
    echo "build.sh is already running. Exiting."
    echo "A bug here: /tmp/gui_build_script.running doesn't get deleted on a script abort. TODO: Fix it."
    exit 1
fi
touch /tmp/gui_build_script.running 


# REBUILD=1
# INSTALL=1
RUN=1
CLEAN=1

echo "Parsing cmdline args..."
for cmdline_args in "$@"
do
	if [ "$cmdline_args" = "--help" ]; then
		echo "Options are: norun and noclean."
		exit 0
	fi
	# if [ "$cmdline_args" == "noinstall" ]; then
	# 	INSTALL=0
	# 	echo "Not installing."
	# fi
	if [ "$cmdline_args" = "norun" ]; then
		RUN=0
		echo "Run disabled."
	fi
	if [ "$cmdline_args" = "noclean" ]; then
		CLEAN=0
		echo "Skipping."
	fi
done


# BEGIN OLD WAY
# # Go to project root directory.
# root_dir=`echo $0 | sed "s/\/ancillary\/build.sh//g"`
# if [ $root_dir == "./build.sh" ]; then 
# 	echo "skip this one $root_dir"
# 	cd ..
# else
# 	cd $root_dir -v
# fi
# END OLD WAY

# New way
# cd project root directory.
cd `echo $0 | sed "s/\/build.sh//g"`/..


echo "Doing out of tree build..."
mkdir	_temp
cd      _temp
cmake	..  || exit 1 # can/will do better later (use dir from $0)  

time make -j2 
# Play an audio if build fails to help morale :).
if [ "$?" = 0 ]; then
	#if [ "$HOME" = "/home/jwicks" ]; then
        #	play /usr/share/sounds/applause.wave  > /dev/null 2>&1  
	#fi
	echo "Make succeeded"
else 
	if [ "$HOME" = "/home/jwicks" ]; then
		play /usr/share/sounds/giveup.ogg > /dev/null 2>&1 
	fi

	rm /tmp/gui_build_script.running
	echo "Make failed."
	exit 1
fi


#if [ $? != 0 ]; then
#        if [ "$HOME" = "/home/jwicks" ]; then
#                play /usr/share/sounds/giveup.ogg
#        fi
#	if [ "$HOME" = "/home/jwicks" ]; then	
#		play /usr/share/sounds/giveup.ogg
#	fi
#fi

sudo	make install || exit 1
cd      ..

# trap ctrl-c 
doCleanup()
{
    if [ $CLEAN -eq 1 ]; then
            rm -rfv _temp
            sudo rm -rfv /srv/CARBLogger_UI
    fi
    
    rm -fv /tmp/gui_build_script.running 
    
    exit 0
}
trap doCleanup INT

echo "Executing CARBLogger_UI..."
if [ $RUN -eq 1 ]; then
	echo "Press ctrl-c to quit carblogger_ui."
	# Need to add options for docroot to default to current directory, if empty.
	# Using main ip address. Haven't tested with multiple NICs.
	/srv/CARBLogger_UI/carblogger_ui --http-address `hostname  -I | cut -f1 -d' '` --http-port 9090
	
	if [ "$?" != 0 ]; then 
		echo "Failed to execute."
		doCleanup
	fi
fi




